﻿using System;
using System.Data;
using System.Windows.Forms;
using AppGauge;
using AppGauge.Data.SqlServer;

namespace AppGauge_CodeGenerator
{
    public partial class Form1 : Form
    {
        DataContainer dc;
        DataTable dt = new DataTable();
        DataTable dTables = new DataTable();
        DataTable dColumns = new DataTable();
        bool LogToggle = true;
        string conStr = @"Data Source=VIKZ91-VAIO\SQLEXPRESS;Initial Catalog=AppGauge;User ID=AppGauge;Password=19Kaaj91";

        public Form1()
        {
            InitializeComponent();
            dc = new DataContainer();
            comboBox1.Text = conStr;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conStr = comboBox1.Text;
            if (conStr == null || conStr.Length < 10 || conStr.Length > 1000)
            {
                Log("Invalid Connection String Provided.");
                return;
            }
            dc.ConnectionString = conStr;
            try
            {
                if (dc.TestConnection())
                {
                    Log("Database Connection OK.");

                    dc.CustomQuery("select table_name from information_schema.tables", ref dTables);
                    dataGridView1.DataSource = dTables;
                    Log(dTables.Rows.Count + " Table(s) Listed");

                    if(checkBox1.Checked==true)
                        button2_Click(this,e);
                }
                else
                {
                    Log("Database Connection failed!");
                    return;
                }
            }
            catch (Exception ex)
            {
                Log("Invalid Connection String");
                AppDebug.Log(ex.ToString());
            }
        }

        private void LogTab_Click(object sender, EventArgs e)
        {
            LogToggle = !LogToggle;

            if (LogToggle)
            {
                LogTab.Text = "Hide";
                LogText.Visible = true;
                this.Height = 405;
            }
            else
            {
                LogTab.Text = "Show";
                LogText.Visible = false;
                this.Height = 335;
            }
        }

        private void GenerateObject(string TableName)
        {
            dt.Clear();
            dc.CustomQuery("select column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='" + TableName + "'", ref dt);

            if (dt.Rows == null || dt.Rows.Count < 1)
            {
                Log("Table/Primary Key not Found!");
                return;
            }

            string Tkey = dt.Rows[0][0].ToString();

            dt.Clear();
            dc.CustomQuery("select column_name,data_type,character_maximum_length,is_nullable from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='" + TableName + "'", ref dColumns);

            if (dColumns.Rows.Count < 1)
            {
                Log("Table not Found!");
                return;
            }

            string fl;
            fl = @"using AppGauge.Data.SqlServer.Fluent;

[Table(""" + TableName + @""")]
class " + TableName + @" : AppData
{";
            for (int i = 0; i < dColumns.Rows.Count; i++)
            {
                fl += "\r\n\t[Column(\"";
                fl += dColumns.Rows[i][0].ToString();
                fl += "\"";
                if (dColumns.Rows[i][0].ToString() == Tkey && dColumns.Rows[i][1].ToString() == "int")
                    fl += ",true";
                fl += ")]";

                fl += "\r\n";
                fl += "\tpublic ";

                if (dColumns.Rows[i][1].ToString() == "int")
                    fl += "int ";
                else if (dColumns.Rows[i][1].ToString() == "numeric")
                    fl += "double ";
                else
                    fl += "string ";

                fl += dColumns.Rows[i][0].ToString() + " ";
                fl += "{get; set;}\r\n";
            }

            fl += "\r\n\tpublic " + TableName + "()";
            fl += "\r\n\t:base(@\"" + conStr + "\")";
            fl += "\r\n\t{\r\n\t}\r\n}";


            string activeDirectory = @"c:\";
            string workPath = System.IO.Path.Combine(activeDirectory, "AppData_Objects");
            System.IO.Directory.CreateDirectory(workPath);

            workPath = System.IO.Path.Combine(workPath, TableName + ".cs");
            System.IO.File.WriteAllText(workPath, fl);
            Log(TableName + ".cs Object Created");
        }

        private void Log(string text)
        {
            DateTime logt = DateTime.Now;
            LogText.Text += "\n$";
            LogText.Text += logt.Hour + "h:" + logt.Minute + "m:" + logt.Second + "s:" + logt.Millisecond + "ms> ";
            LogText.Text += text;
            LogText.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dTables.Rows.Count < 1)
            {
                Log("No Table Found!");
                return;
            }

            for (int i = 0; i < dTables.Rows.Count; i++)
                GenerateObject(dTables.Rows[i][0].ToString());
        }
    }
}
